/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ULHT.LIG2N1.RC.VO;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Administrador
 */
public class BlacklistVO {

    private ArrayList<String> Blacklist;
    private final String filename = "BlackList.Data";

    public String getFilename() {
        return filename;
    }

    public BlacklistVO(ArrayList<String> Blacklist) {
        this.Blacklist = Blacklist;
    }

    public BlacklistVO() {
        try {
            this.Blacklist = loadBlackList();
        } catch (FileNotFoundException ex) {
            this.Blacklist = new ArrayList<>();
        } catch (IOException ex) {
            this.Blacklist = new ArrayList<>();
        }
    }

    public void addToBlacklist(String ipAddress) {
        this.Blacklist.add(ipAddress);
    }

    public void removeFromBlacklist(String ipAddress) {
        if (Blacklist.contains(ipAddress)) {
            Blacklist.remove(ipAddress);
        }
    }

    public boolean isBlacklisted(String ipAddress) {
        return Blacklist.contains(ipAddress);
    }

    public ArrayList<String> getBlacklist() {
        return Blacklist;
    }

    public ArrayList<String> loadBlackList() throws FileNotFoundException, IOException {
        FileManage fileManage = new FileManage();
        return fileManage.readArrayListFromFile(this.filename);
    }

    private void writeBlackList() throws FileNotFoundException, IOException {
        FileManage fileManage = new FileManage();
        fileManage.writeArrayListToFile(this.Blacklist, this.filename);
    }
    
    public void persistBlackList(){
        try {
            writeBlackList();
        } catch (FileNotFoundException ex) {
            System.out.println("Erro ao escrever Whitelist");
        } catch (IOException ex) {
            System.out.println("Erro ao escrever Whitelist");
        }
    }
    
    public String getBlackListStr(){
        String blacklistStr = "";
        Iterator itr = Blacklist.iterator();
        while(itr.hasNext()) {
            blacklistStr += itr.next() + "\n";
        }        
        if (blacklistStr.equals(""))
            blacklistStr = "Blacklist vazia\n";
        return blacklistStr;
    }
}
